package com.base.myproject.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.myproject.client.system.Env;

@SuppressWarnings("serial")
public class TaskServlet extends HttpServlet {

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// response.setContentType("application/json;charset=UTF-8");
		response.setContentType("multipart/form-data;charset=UTF-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		Enumeration  enumeration=request.getParameterNames();
		
		while(enumeration.hasMoreElements())
		{
			String  eeee =""+ enumeration.nextElement();
			System.out.print("e="+eeee+"=");
			System.out.println(request.getParameter(""+eeee));
		}
		String action =request.getParameter("Action");
		if(action!=null && action.equals("Loaddata"))
		{
		out.println(Task.getinstance().P2XMLSTR());
	
		}
		
		else if (action != null && action.equals("save")) {
			String taskname =request.getParameter("taskname");
			String bcode =request.getParameter("bcode");
			String icode =request.getParameter("icode");
			String subject =request.getParameter("subject");
			String fromaddr =request.getParameter("fromaddr");
			String toaddr =request.getParameter("toaddr");
			String status =request.getParameter("status");


			//如果icode为空这这是增加，否则为修改
			if((icode==null ||icode.equals("null")||icode.equals("")))
			{
				System.out.println("insert.............");
			java.util.Hashtable<String, String> ht = new java.util.Hashtable<String, String>();
			ht.put("bcode", bcode==null?"":bcode);
			ht.put("taskname",  taskname==null?"":taskname);
			ht.put("subject", subject==null?"":subject);
			ht.put("fromaddr",  fromaddr==null?"":fromaddr);
			ht.put("toaddr",  toaddr==null?"":toaddr);
			ht.put("status",  status==null?"":status);
			
			Task.getinstance().add(ht);
			out.print(Env.SUCCESS);
			}
			else
			{
				System.out.println("edit.............");
				java.util.Hashtable<String, String> ht = (java.util.Hashtable<String, String> )Task.getinstance().getP().get(Integer.parseInt(icode));
				if(taskname!=null)
				{
					ht.put("taskname", taskname);
				}
				if(bcode!=null)
				{
					ht.put("bcode", bcode);
				}
				if(subject!=null)
				{
					ht.put("subject", subject);
				}
				if(fromaddr!=null)
				{
					ht.put("fromaddr", fromaddr);
				}
				if(toaddr!=null)
				{
					ht.put("toaddr", toaddr);
				}
				
				if(status!=null)
				{
					ht.put("status", status);
				}
				Task.getinstance().update(Integer.parseInt(icode), ht);
				out.print(Env.SUCCESS);
			}
		}
		
		else if (action != null && action.equals("add")) {
			
		}
		
		else if (action != null && action.equals("delete")) {
			
		}
		
		out.flush();
		out.close();
/*		java.util.Hashtable<String, String> ht = new java.util.Hashtable<String, String>();
		ht.put("bcode", "zgw");
		ht.put("taskname", "zgw");
		ht.put("predate", "zgw");
		ht.put("modifydate", "zgw");
		Task.getinstance().add(ht);
		Task.getinstance().save();*/
	}
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}
		
}
